<?php
namespace Crm\Table;

use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;

class ProductTable extends AbstractTable
{

    protected $table = 'hy_product';

    protected $id = 'product_id';
    
    /**
     * 查询 包括 配餐 和 商城 的商品
     * @param array $query
     */
    public function find($query = array())
    {
        $select = new Select(array(
            'p' => $this->getTable()
        ));
        // $select->columns(array(''));
        $select->join(array(
            'o' => 'hy_organization'
        ), 'o.org_id=p.org_id', array(
            'org_name'
        ), $select::JOIN_LEFT);
        if ($query['org_id']) {
            $select->where(array(
                'p.org_id' => $query['org_id']
            ));
        }
        
        if (! empty($query['product_name'])) {
            $spec = function (Where $where) use($query) {
                $where->like('p.product_name', "%" . $query['product_name'] . "%");
            };
            
            $select->where($spec);
        }
        
        $select->order('p.product_id DESC');
        return $this->fetchPaginate($select, $query);
    }
    
    /**
     * 商城商品  product_type = 2
     * @param array $query
     */
    public function findShopGoods($query = array())
    {
        $select = new Select(array(
            'p' => $this->getTable()
        ));
        // $select->columns(array(''));
        $select->join(array(
            'o' => 'hy_organization'
        ), 'o.org_id=p.org_id', array(
            'org_name'
        ), $select::JOIN_LEFT);
        
        $select->join(array(
            'c' => 'hy_product_category'
        ), 'c.product_id=p.product_id', array(
            'category_id'
        ), $select::JOIN_LEFT);
        
        $select->join(array(
            'g' => 'hy_category'
        ), 'g.category_id=c.category_id', array(
            'category_name'
        ), $select::JOIN_LEFT);
        
        
        if ($query['org_id']) {
            $select->where(array(
                'p.org_id' => $query['org_id']
            ));
        }
    
        if (! empty($query['product_name'])) {
            $spec = function (Where $where) use($query) {
                $where->like('p.product_name', "%" . $query['product_name'] . "%");
            };
    
            $select->where($spec);
        }
    
        $select->where(array(
            'p.product_type' => 2
        ));
        
        if ($query['admin_org_id']) {
            $select->where(array(
                'p.org_id' => $query['admin_org_id']
            ));
        }
        
        $select->order('p.product_id DESC');
        return $this->fetchPaginate($select, $query);
    }
    
    /**
     * 配餐商品  product_type = 1
     * @param array $query
     */
    public function findOrgGoods($query = array())
    {
        $select = new Select(array(
            'p' => $this->getTable()
        ));
        // $select->columns(array(''));
        $select->join(array(
            'o' => 'hy_organization'
        ), 'o.org_id=p.org_id', array(
            'org_name', 'org_is_direct_sale'
        ), $select::JOIN_LEFT);
        if ($query['org_id']) {
            $select->where(array(
                'p.org_id' => $query['org_id']
            ));
        }
    
        if (! empty($query['product_name'])) {
            $spec = function (Where $where) use($query) {
                $where->like('p.product_name', "%" . $query['product_name'] . "%");
            };
    
            $select->where($spec);
        }
    
        $select->where(array(
            'p.product_type' => 1
        ));
    
        if ($query['admin_org_id']) {
            $select->where(array(
                'p.org_id' => $query['admin_org_id']
            ));
        }
        
        $select->order('p.product_id DESC');
        return $this->fetchPaginate($select, $query);
    }

    public function findAll()
    {
        $rowset = $this->select();
        $result = $rowset->toArray();
        return $result;
    }
    
    /**
     * 指定商户 商品列表  暂未设置条件
     */
    public function findAllByOrgId($org_id = 0)
    {
        $result = '';
        $where = array();
        
        if($org_id){
            $where['org_id'] = $org_id;
            $rowset = $this->select($where);
            $result = $rowset->toArray();
        }
        
        return $result;
        
        
        
//         $select = new Select(array(
//             'p' => $this->getTable()
//         ));
//         // $select->columns(array(''));
//         if ($org_id) {
//             $select->where(array(
//                 'p.org_id' => $org_id
//             ));
//         }
        
        
        
    }

    public function findOne($id)
    {
        $rowset = $this->select(array(
            $this->getId() => $id
        ));
        $row = $rowset->current();
        return $row;
    }
    
    public function findOneToArray($id)
    {
        $rowset = $this->select(array(
            $this->getId() => $id
        ));
        
        $row = $rowset->toArray();
        return $row;
    }
    
    /**
     * 商城 有分类
     * @param unknown $id
     */
    public function findOneShopToArray($product_id)
    {
//         $rowset = $this->select(array(
//             $this->getId() => $product_id
//         ));
    
//         $row = $rowset->toArray();
//         return $row;
        
        
        $select = new Select(array(
            'p' => $this->getTable()
        ));
        //         $select->columns(array(
        //             'product_id'
        //         ));
        
        $select->join(array(
            'c' => 'hy_product_category'
        ), 'c.product_id=p.product_id', array(
            'category_id'
        ), $select::JOIN_LEFT);
        

        $select->where(array(
            'p.product_id' => $product_id
        ));

        $rowset = $this->selectWith($select);
        $result = $rowset->toArray();
        return $result;
        
        
    }
    
    /**
     * 监管平台
     */
    public function findAllSupervision($query = array())
    {
    
        $select = new Select(array(
            'p' => $this->getTable()
        ));
//         $select->columns(array(
//             'product_id'
//         ));
    
        $select->join(array(
            'o' => 'hy_organization'
        ), 'o.org_id=p.org_id', array(
            'org_name'
        ), $select::JOIN_LEFT);
    
        $select->order('p.product_id DESC');
    
        return $this->fetchPaginate($select, $query);
    }

    public function create($data)
    {
        $this->insert($data);
        $id = $this->lastInsertValue;
        
        return $id;
    }

    public function updateById($data, $id)
    {
        return $this->update($data, array(
            $this->getId() => $id
        ));
    }

    public function remove()
    {}
    
    public function findByOrgId($org_id)
    {
        $rowset = $this->select(array(
            'org_id' => $org_id
        ));
        $row = $rowset->toArray();
        return $row;
    }
}